package com.datastructure.test.minoverridechildserial;

public class MinOverrideChildSerial {

    public static void main(String[] args) {
        String S = "vwvefvreeq";
        String T = "v";
        MinOverrideChildSerial minOverrideChildSerial = new MinOverrideChildSerial();
        System.out.println(minOverrideChildSerial.minWindow(S,T));
    }

    public String minWindow (String S, String T) {
        // write code here
        if(S.length()<T.length()){
            return "";
        }
        if(S.length()==T.length()&&!S.equals(T)){
            return "";
        }
        if(S.equals(T)){
            return S;
        }
        boolean flag;
        String[] split = T.split("");
        int lengthOfTemp = T.length();
        String temp = "";
        String temp3 = "";
        while (lengthOfTemp<=S.length()) {
            for (int i = 0; i < S.length()-lengthOfTemp+1; i++) {
                flag=true;
                temp=S.substring(i,i+lengthOfTemp);
                temp3=temp;
                if(temp.length()>=T.length()) {
                    for (String aByte : split) {
                        if (!temp.contains(aByte)) {
                            flag = false;
                            break;
                        } else {
                            String[] strings = temp.split("");
                            int indexJ=0;
                            for (int j = 0; j < strings.length; j++) {
                                if(strings[j].equals(aByte)){
                                    indexJ=j;
                                    break;
                                }
                            }
                            String temp1="";
                            String temp2="";
                            if(indexJ<=temp.length()){
                                temp1=temp.substring(0,indexJ);
                            }
                            if(indexJ+1<=temp.length()){
                                temp2=temp.substring(indexJ+1);
                            }
                            temp = temp1+temp2;
                        }
                    }
                }
                temp=temp3;
                if(flag){
                    return temp;
                }
            }
            lengthOfTemp++;
        }
        return "";
    }

}
